import { http } from './request.js';

// 获取订单预览信息（从购物车创建）
export function previewOrderFromCart(cartItemIds) {
    return http.get('/user/orders/preview/cart', { cartItemIds });
}

// 获取订单预览信息（直接购买）
export function previewOrderFromProduct(productId, skuId, quantity) {
    return http.get('/user/orders/preview/product', { productId, skuId, quantity });
}

// 创建订单
export function createOrder(data) {
    return http.post('/user/orders', data);
}

// 获取订单列表
export function getOrders(params) {
    return http.get('/user/orders', params);
}

// 获取订单详情
export function getOrderDetail(id) {
    return http.get(`/user/orders/${id}`);
}

// 取消订单
export function cancelOrder(id, reason) {
    return http.post(`/user/orders/${id}/cancel`, null, { params: { reason } });
}

// 确认收货
export function completeOrder(id) {
    return http.post(`/user/orders/${id}/complete`);
}

// 删除订单
export function deleteOrder(id) {
    return http.delete(`/user/orders/${id}`);
}

// 支付订单
export function payOrder(id, paymentMethod) {
    return http.post(`/user/orders/${id}/pay`, null, { params: { paymentMethod } });
}

// 获取订单物流信息
export function getOrderLogistics(id) {
    return http.get(`/user/orders/${id}/logistics`);
}

// 获取待支付订单数量
export function getUnpaidCount() {
    return http.get('/user/orders/unpaid/count');
}

// 获取订单统计
export function getOrderStats() {
    return http.get('/user/orders/stats');
}

// 获取待付款订单
export function getPendingPaymentOrders() {
    return http.get('/user/orders/pending-payment');
}

// 获取待发货订单
export function getPendingShipmentOrders() {
    return http.get('/user/orders/pending-shipment');
}

// 获取待收货订单
export function getPendingReceiveOrders() {
    return http.get('/user/orders/pending-receive');
}

// 申请退款
export function applyRefund(id, data) {
    return http.post(`/user/orders/${id}/refund`, data);
}

// 获取骑手位置
export const getRiderLocation = (orderId) => {
    return http.get(`/orders/${orderId}/rider-location`);
};

// 刷新订单配送信息
export const refreshDeliveryInfo = (orderId) => {
    return http.get(`/orders/${orderId}/delivery-info`);
};

// 再次购买
export function buyAgain(orderId) {
    return http.post(`/user/orders/${orderId}/buy-again`);
}

// 确认收货
export function confirmOrder(orderId) {
    return http.post(`/user/orders/${orderId}/complete`);
}

// 确认自提
export function confirmSelfPickup(orderId) {
    return http.post(`/user/orders/${orderId}/self-pickup`);
}

// 导出订单API对象，与其他API文件保持一致的导出方式
export const orderApi = {
    previewOrderFromCart,
    previewOrderFromProduct,
    createOrder,
    getOrders,
    getOrderDetail,
    cancelOrder,
    completeOrder,
    deleteOrder,
    payOrder,
    getOrderLogistics,
    getUnpaidCount,
    getOrderStats,
    getPendingPaymentOrders,
    getPendingShipmentOrders,
    getPendingReceiveOrders,
    applyRefund,
    getRiderLocation,
    refreshDeliveryInfo,
    buyAgain,
    confirmOrder,
    confirmSelfPickup
}; 